<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller{
	
	
	protected $processedPass;
	public function __construct(){
		parent::__construct();
		$this->load->model("m_user");
		$this->arrUserSess = $this->session->userdata("session_user");
	}
	
	public function index(){

		if($this->isLoggedIn()){
			redirect("backend/home");
		}else{
			$this->viewLogin();
		}
	}

	public function viewLogin($paramData = array()){
		$data["title"] = "Login";
		$data = array_merge($data, $paramData);
		$this->load->view("backend/login", $data);
	}

	

	public function loginProcess(){
		$username = $this->input->post("moslem_username");
		$password = $this->hashPass($this->input->post("moslem_password"));
		
		$this->form_validation->set_error_delimiters('<li>','</li>');
		$this->form_validation->set_rules('moslem_username', 'Username', 'callback_loginCheck');
		$this->form_validation->set_rules('moslem_password','Password','callback_passwordCheck');
		

		if($this->form_validation->run() == FALSE){
			$this->arrUserSess = array();
			$this->session->unset_userdata("session_user");
			$this->session->set_userdata("session_user",$this->arrUserSess);
			$data['status'] = "alert-danger";
			$this->viewLogin($data);
		}else{
			$arrUserData = $this->m_user->getOneById($username);
			$this->session->set_userdata("session_user",$arrUserData);
			$this->arrUserSess = $this->session->userdata("session_user");
			redirect("backend/home/");
		}

	}

	public function loginCheck($username){
		$arrUserData = $this->m_user->getOneById($username);
		
		if(empty($arrUserData)){
			$this->form_validation->set_message('loginCheck', 'User with that Username not found');
			return FALSE;
		}else{
			$this->processedPass = $arrUserData["password"];
			return TRUE;
		}
	}

	public function passwordCheck($strPassword){
		if($this->processedPass != $this->hashPass($strPassword)){
			$this->form_validation->set_message('passwordCheck', 'Password not match');
			return FALSE;
		} else {
			return TRUE;
		}
		
	}

	public function logoutProcess(){
		$this->session->unset_userdata("session_user");
		$this->arrUserSess = array();
		$this->viewLogin();
	}


	public function lists(){
		$this->load->model("m_role");

		$this->data["admin"] = $this->m_user->getAll();
		$this->data["role"]	 = $this->m_role->getAll();
		$this->data["title"] = "Admin";
		$this->data['page']  = 'admin';
		
		$this->load->view('backend/dashboard', $this->data);
	}
	
	private function hashPass($password){
		$salt = "Moslem";
		return md5($salt . $password);
	}

	public function createUser(){

		$this->data['page'] 	= 'add_admin';
		$this->data["title"]	= "Add Admin";

		$this->load->view('backend/dashboard', $this->data);
		/*$strUsername 	= $this->input->post("username");
		$strEmail 		= $this->input->post("email");
		$strFullName 	= $this->input->post("full-name");
		$strIdJabatan 	= $this->input->post("role");

		$this->form_validation->set_error_delimiters('<li>','</li>');
		$this->form_validation->set_rules('username', 'Username', 'callback_usernameCheck');
		$this->form_validation->set_rules('email', 'E-mail', 'callback_emailCheck');

		if($this->form_validation->run() == FALSE){
			$this->lists("alert-danger");
		}else{
			$arrParam = array($strUsername, $this->hashPass("moslem"), $strEmail, $strFullName, $strIdJabatan);
			if($this->m_user->insertUser($arrParam)){
				$this->lists("alert-success", "Proses Insert berhasil");
			}else{
				$this->lists("alert-danger", "Proses Insert gagal");
			}
		}*/
	}

	public function usernameCheck($strUsername)
	{
		$arrUser = $this->m_user->getOneById($strUsername);
		if(!empty($arrUser)){
			$this->form_validation->set_message('usernameCheck', 'Username already taken');
			return FALSE;
		}else{

			return TRUE;
		}
	}

	public function emailCheck($strEmail)
	{
		$arrUser = $this->m_user->getOneByEmail($strEmail);
		if(!empty($arrUser)){
			$this->form_validation->set_message('emailCheck', 'E-mail already registered');
			return FALSE;
		}else{
			return TRUE;
		}
	}

	public function removeUser($strId){
		if($this->m_user->deleteUser($strId)){
			$this->lists("alert-success", "Proses Delete berhasil");
		}else{
			$this->lists("alert-danger", "Proses delete gagal");
		}
	}

	public function resetPassword($strId){
		$arrParam = array(
			$this->hashPass("moslem"),
			$strId
		);
		if($this->m_user->resetPassword($arrParam)){
			$this->lists("alert-success", "Reset password berhasil");
		}else{
			$this->lists("alert-danger", "Reset password  gagal");
		}
	}

}
/* End of file user.php */
/* Location: .controllers/backend/user.php */